#!/bin/bash

CUR_DIR="$( cd "$( dirname "$(readlink -f "$0")" )" && pwd )"

source $CUR_DIR/../cluster_IP.conf

ORG_ID=0 # bc-storage cluster org id, which is globally unique, like 0-3

FSID=`uuidgen`
hostname=$(hostname -s)
USER=$(whoami)

# CEPH_IP 的 ORG_ID 下标为 MON_HOST
MON_HOST=${CEPH_IPs[$ORG_ID]}

gen_conf() {
    echo "[global]
    fsid = $FSID
    cluster id = 1
    fabric kafka brokers = '192.168.1.101:9092'
    fabric kafka topic = 'ceph'
    mon initial members = $hostname
    mon host = $MON_HOST
    # public network = '127.0.0.0/24'
    osd journal size = 1024
    osd pool default size = 1
    osd pool default min size = 1
    osd pool default pg num = 128
    osd pool default pgp num = 128
    osd crush chooseleaf type = 0
    mon allow pool delete = true
    # mon max pg per osd = 1000
    " | tee -a /etc/ceph/ceph.conf
}

stop() {
    echo "[$hostname] stop ceph-mon in $hostname ..."
    sudo kill -9 `ps -ef | grep ceph-mon | grep -v grep | awk '{print $2}'`
    sudo kill -9 `ps -ef | grep ceph-mgr | grep -v grep | awk '{print $2}'`
    sudo kill -9 `ps -ef | grep ceph-osd | grep -v grep | awk '{print $2}'`
    # for i in `seq 0 2`; do
    #     sudo umount /var/lib/ceph/osd/ceph-$i
    # done
    sleep 5
    sudo umount /dev/sdb
    sudo umount /dev/sdc
    sudo umount /dev/sdd
    sudo rm -rf /tmp/ceph.mon.keyring /tmp/monmap /var/lib/ceph/* /etc/ceph/*
    echo "[$hostname] finish stop ceph-mon in $hostname"
}

start_mon() {
    echo "[$hostname] start ceph-mon in $hostname ..."

    sudo mkdir -p /etc/ceph && sudo chown -R $USER:$USER /etc/ceph
    sudo mkdir -p /var/lib/ceph && sudo chown -R $USER:$USER /var/lib/ceph

    ceph-authtool --create-keyring /tmp/ceph.mon.keyring --gen-key -n mon. --cap mon 'allow *'
    ceph-authtool --create-keyring /etc/ceph/ceph.client.admin.keyring --gen-key -n client.admin --cap mon 'allow *' --cap osd 'allow *' --cap mds 'allow *' --cap mgr 'allow *'
    mkdir -p /var/lib/ceph/bootstrap-osd
    ceph-authtool --create-keyring /var/lib/ceph/bootstrap-osd/ceph.keyring --gen-key -n client.bootstrap-osd --cap mon 'profile bootstrap-osd' --cap mgr 'allow r'
    ceph-authtool /tmp/ceph.mon.keyring --import-keyring /etc/ceph/ceph.client.admin.keyring
    ceph-authtool /tmp/ceph.mon.keyring --import-keyring /var/lib/ceph/bootstrap-osd/ceph.keyring
    # chown $USER:$USER /tmp/ceph.mon.keyring
    # chown -R $USER:$USER /var/lib/ceph
    # chown -R $USER:$USER /etc/ceph/
    sudo -u $USER monmaptool --create --add $hostname $MON_HOST --fsid $FSID /tmp/monmap --clobber
    mkdir -p /var/lib/ceph/mon/ceph-$hostname
    sudo -u $USER ceph-mon --mkfs -i $hostname --monmap /tmp/monmap --keyring /tmp/ceph.mon.keyring
    sudo chown -R $USER:$USER /var/run/ceph
    sudo -u $USER ceph-mon -i $hostname
    echo "[$hostname] finish start ceph-mon in $hostname"

    # deal warning
    ceph config set mon auth_allow_insecure_global_id_reclaim false
    ceph mon enable-msgr2
}

start_mgr() {
   echo "[$hostname] start ceph-mgr in $hostname ..."
   mkdir -p /var/lib/ceph/mgr/ceph-$hostname
   ceph auth get-or-create mgr.$hostname mon 'allow profile mgr' osd 'allow *' mds 'allow *' | sudo tee -a /var/lib/ceph/mgr/ceph-$hostname/keyring 
   sudo -u $USER ceph-mgr -i $hostname
   echo "[$hostname] finish start ceph-mgr in $hostname"
}

start_osd() {
   echo "[$hostname] start ceph-osd in $hostname ..."
   HDD=$1
   OSDID=`uuidgen`
   OSD_SECRET=$(ceph-authtool --gen-print-key)
   OSD_NUM=$(echo "{\"cephx_secret\": \"$OSD_SECRET\"}" | \
      ceph osd new $OSDID -i - \
      -n client.bootstrap-osd -k /var/lib/ceph/bootstrap-osd/ceph.keyring)
   echo "[$hostname] get osd.num = $OSD_NUM ..."
   sudo mkdir -p /var/lib/ceph/osd/ceph-$OSD_NUM
   sudo mkfs.ext4 /dev/$HDD > /dev/null
   sudo mount /dev/$HDD /var/lib/ceph/osd/ceph-$OSD_NUM > /dev/null
   sudo ceph-authtool --create-keyring /var/lib/ceph/osd/ceph-$OSD_NUM/keyring --name osd.$OSD_NUM --add-key $OSD_SECRET | sed "s/^/[$hostname] &/g"
   sudo ceph-osd -i $OSD_NUM --mkfs --osd-uuid $OSDID > /dev/null 2>&1
   sudo ceph-osd -i $OSD_NUM > /dev/null 2>&1

    # OSD_NUM=0
    # sudo mkdir -p /var/lib/ceph/osd/ceph-$OSD_NUM
    # sudo mkfs -t ext4 /dev/$HDD  > /dev/null
    # sudo mount -o user_xattr /dev/$HDD /var/lib/ceph/osd/ceph-$OSD_NUM
    # sudo ceph-osd -i $OSD_NUM --mkfs --mkkey
    # ceph auth add osd.$OSD_NUM osd 'allow *' mon 'allow rwx' -i /var/lib/ceph/osd/ceph-$OSD_NUM/keyring
    # ceph osd crush add $OSD_NUM 1.0 host=$hostname
    # sudo systemctl start ceph-osd@$OSD_NUM

   echo "[$hostname] finish start ceph-osd.$OSD_NUM in $hostname"
}

some_conf() {
    ceph config set global mon_warn_on_pool_no_redundancy false

}


set -x

stop

# gen_conf

# start_mon
# start_mgr
# start_osd sdb
# start_osd sdc
# start_osd sdd